using ASP;
using ClassLibrary.BusinessLayer.Facade;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;

public partial class HeadingSetTmplt : Page, IRequiresSessionState
{

    private void InitddlSection()
    {
        /*
        ListItem item = new ListItem();
        item.Text = "";
        item.Value = "";
        ddlSection.Items.Add(item);
        
        ListItem item1 = new ListItem();
        item1.Text = "Pre-Filing";
        item1.Value = "1";
        ddlSection.Items.Add(item1);

        ListItem item2 = new ListItem();
        item2.Text = "Post-Filing";
        item2.Value = "2";
        ddlSection.Items.Add(item2);

        ListItem item3 = new ListItem();
        item3.Text = "Official Actions";
        item3.Value = "3";
        ddlSection.Items.Add(item3);

        ListItem item4 = new ListItem();
        item4.Text = "Finance";
        item4.Value = "4";
        ddlSection.Items.Add(item4);
         */

        IPTableAdapters.HeadingSectionTableAdapter ct = new IPTableAdapters.HeadingSectionTableAdapter();
        DataTable dt = ct.GetHeadingSectionData();
        ddlSection.DataSource = dt;
        ddlSection.DataTextField = "Description";
        ddlSection.DataValueField = "Section";
        ddlSection.DataBind();
        ddlSection.Items.Insert(0, new ListItem("", ""));
    }
    private void SetddlSection(string val)
    {
        for (int i = 0; i < ddlSection.Items.Count; i++)
        {
            if (ddlSection.Items[i].Value == val)
            {
                ddlSection.SelectedIndex = i;
                break;
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.setButtons(string.Empty);
        this.setTextReadOnly(true);
        this.clearText();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSetTemplate");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            try
            {
                string strSection = ddlSection.SelectedItem.Value.ToString().Trim();
                this.ODSHeadingSetTmplt.DeleteMethod = "Delete";
                this.ODSHeadingSetTmplt.DeleteParameters["Original_HeadingSet"].DefaultValue = this.ddlHeadingSet.SelectedValue.ToString();
                this.ODSHeadingSetTmplt.DeleteParameters["Original_HeadingCode"].DefaultValue = this.txtHeadingTemplate.Text;
                this.ODSHeadingSetTmplt.DeleteParameters["Original_Section"].DefaultValue = strSection;
                this.ODSHeadingSetTmplt.Delete();
                this.setButtons(string.Empty);
                this.setTextReadOnly(true);
                this.clearText();
                this.Search("");
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.chkForOperation.Checked = true;
            }
        }
    }

    protected void btnHeadingTemplateSearch_Click(object sender, EventArgs e)
    {
        if ((this.Session["SelectedHeadingCode"] != null) && (this.Session["SelectedHeadingCode"].ToString() != ""))
        {
            this.txtHeadingTemplate.Text = this.Session["SelectedHeadingCode"].ToString();
            //this.txtSection.Text = this.Session["SelectedHeadingSection"].ToString();
            SetddlSection(this.Session["SelectedHeadingSection"].ToString());
            this.Session.Remove("SelectedHeadingCode");
            this.Session.Remove("SelectedHeadingSection");
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSetTemplate");
        if (!canDelete.CanUpdate())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoEdit.ToString();
        }
        else
        {
            this.setButtons("modify");
            this.setTextReadOnly(true);
            //this.txtSection.ReadOnly = false;
            //this.txtSection.Enabled = true;
            this.ddlSection.Enabled = true;
            this.ddlHeadingSet.Focus();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSetTemplate");
        if (!canDelete.CanAdd())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoAdd.ToString();
        }
        else
        {
            this.clearText();
            this.setButtons("new");
            this.cbForUpdate.Checked = false;
            this.setTextReadOnly(false);
            this.ddlHeadingSet.Focus();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string strSection = ddlSection.SelectedItem.Value.ToString().Trim();
            if ((((strSection != "1") & (strSection != "2")) & (strSection != "3")) & (strSection != "4"))
            {
                throw new Exception(Strings.SectionRange.ToString());
            }
            if (!this.cbForUpdate.Checked)
            {
                this.ODSHeadingSetTmplt.InsertMethod = "Insert";
                this.ODSHeadingSetTmplt.InsertParameters["HeadingSet"].DefaultValue = this.ddlHeadingSet.SelectedValue.ToString();
                this.ODSHeadingSetTmplt.InsertParameters["HeadingCode"].DefaultValue = this.txtHeadingTemplate.Text;
                this.ODSHeadingSetTmplt.InsertParameters["Section"].DefaultValue = strSection;
                this.ODSHeadingSetTmplt.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSHeadingSetTmplt.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSHeadingSetTmplt.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSHeadingSetTmplt.Insert();
            }
            else
            {
                this.ODSHeadingSetTmplt.UpdateMethod = "Update";
                this.ODSHeadingSetTmplt.UpdateParameters["Original_HeadingSet"].DefaultValue = this.ddlHeadingSet.SelectedValue.ToString();
                this.ODSHeadingSetTmplt.UpdateParameters["Original_HeadingCode"].DefaultValue = this.txtHeadingTemplate.Text;
                this.ODSHeadingSetTmplt.UpdateParameters["Original_Section"].DefaultValue = this.txtSection2.Text.ToString();
                this.ODSHeadingSetTmplt.UpdateParameters["Section"].DefaultValue = strSection;
                this.ODSHeadingSetTmplt.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSHeadingSetTmplt.UpdateParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSHeadingSetTmplt.Update();
            }

            this.setButtons(string.Empty);
            this.setTextReadOnly(true);
            this.clearText();
            this.Search("");
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            this.chkForOperation.Checked = true;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.Search("");
    }

    protected void clearText()
    {
        this.txtHeadingTemplate.Text = string.Empty;
        this.chkForOperation.Checked = false;
        SetddlSection("0");
        this.lblError.Text = "";
    }

    protected void ddlHeadingSet_PreRender(object sender, EventArgs e)
    {
        try
        {
            this.ddlHeadingSet.SelectedIndex = -1;
            this.ddlHeadingSet.Items.FindByValue(this.gvHST.DataKeys[this.gvHST.SelectedIndex].Values["HeadingSet"].ToString()).Selected = true;
        }
        catch (NullReferenceException)
        {
        }
        catch (ArgumentOutOfRangeException)
        {
        }
    }

    protected void gvHST_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvHST.PageIndex = e.NewPageIndex;
        this.gvHST.DataBind();
        this.Search("");
    }

    protected void gvHST_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.clearText();
        this.chkForOperation.Checked = true;
        this.cbForUpdate.Checked = true;
        this.txtHeadingTemplate.Text = this.gvHST.DataKeys[this.gvHST.SelectedIndex].Values["HeadingCode"].ToString();
        //this.txtSection.Text = this.gvHST.DataKeys[this.gvHST.SelectedIndex].Values["Section"].ToString();
        this.txtSection2.Text = this.gvHST.DataKeys[this.gvHST.SelectedIndex].Values["Section"].ToString();
        this.setButtons(string.Empty);
        SetddlSection(this.txtSection2.Text);
    }

    protected void gvHST_Sorting(object sender, GridViewSortEventArgs e)
    {
        this.Search(e.SortExpression);
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }

        ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.ProcedureSetup + " | " + ISitemap.ProcedureSetTemplate;

        this.setButtons(string.Empty);
        this.setTextReadOnly(true);
        InitddlSection();
        this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");

        if (base.Request.QueryString["HeadingSet"] != "")
        {
            this.txtSearchProcSet.Text = base.Server.UrlDecode(base.Request.QueryString["HeadingSet"]);
        }

        this.Search("");
    }

    protected void Search(string SortExpression)
    {
        /*
        ProcedureSetTmpltFacade facade = new ProcedureSetTmpltFacade();
        SqlParameter[] sqlparam = new SqlParameter[2];
        sqlparam[0] = new SqlParameter();
        sqlparam[0].ParameterName = "@HeadingSet";
        sqlparam[0].Value = this.txtSearchProcSet.Text.ToString();
        sqlparam[1] = new SqlParameter();
        sqlparam[1].ParameterName = "@HeadingCode";
        sqlparam[1].Value = this.txtSearchEProcCode.Text.ToString();
        DbParameter[] param = sqlparam;
        DataView dv = facade.GetsProcedureSetTmplt("_IPMS_HeadingSetTemplateSearch_SP", param).DefaultView;
        dv.Sort = SortExpression;
        this.gvHST.DataSourceID = string.Empty;
        this.gvHST.DataSource = dv;
        this.gvHST.DataBind();
        */
        IPTableAdapters.HeadingSetTemplateTableAdapter htta = new IPTableAdapters.HeadingSetTemplateTableAdapter();

        DataTable dt = htta.GetHeadingSetTemplateDatabyljr(this.txtSearchProcSet.Text.ToString(), this.txtSearchEProcCode.Text.ToString());
        this.gvHST.DataSourceID = string.Empty;
        this.gvHST.DataSource = dt;
        this.gvHST.DataBind();
    }

    public void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                this.btnHeadingTemplateSearch.Enabled = true;
                return;

            case "modify":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                this.btnHeadingTemplateSearch.Enabled = false;
                return;
        }
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtSection2.Text.Length != 0;
        this.btnSave.Enabled = false;
        this.btnDelete.Enabled = this.txtSection2.Text.Length != 0;
        this.btnHeadingTemplateSearch.Enabled = false;
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.ddlHeadingSet.Enabled = !bBool;
        this.txtHeadingTemplate.Enabled = !bBool;
        //this.txtSection.ReadOnly = bBool;
        this.ddlSection.Enabled = !bBool;
    }

}
